package com.heifeng.cloudshelf.base;

import android.content.Context;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.android.tu.loadingdialog.LoadingDialog;
import com.gyf.immersionbar.ImmersionBar;

import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * 无MVP的Fragment基类
 */

public abstract class SimpleFragment extends Fragment implements IBaseView {
    public String TAG = "Test";
    protected View mView;
    protected Context mContext;
    private Unbinder mUnBinder;

    public LoadingDialog dialog;
    LoadingDialog.Builder builder;

    @Override
    public void onAttach(Context context) {
        mContext = getActivity();
        super.onAttach(context);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mView = inflater.inflate(getLayoutId(), null);
        return mView;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mUnBinder = ButterKnife.bind(this, view);
        //初始化沉浸式
        if (isImmersionBarEnabled()) {
            initImmersionBar();
        }
        initEventAndData();
    }

    /**
     * 是否可以使用沉浸式
     *
     * @return the boolean
     */
    protected boolean isImmersionBarEnabled() {
        return true;
    }

    /**
     * 初始化沉浸式
     */
    protected void initImmersionBar() {
        //沉浸式状态栏
        //设置状态栏字体为黑色
/*        StatusBarCompat.appOverlayStatusBar(getActivity(), true, true);
        //虚拟按键
        StatusBar.transportStatus(getActivity());*/

        //设置状态栏样式
        ImmersionBar.with(this)
                .transparentStatusBar()  //透明状态栏，不写默认透明色
//                .statusBarColor(R.color.colorPrimary)  //指定状态栏颜色,根据情况是否设置
//                .statusBarAlpha(0.2f)  //状态栏透明度，不写默认0.0f
                .statusBarDarkFont(true, 0.5f)//状态栏字体是深色，不写默认为亮色
                .init();

/*        //设置导航栏样式
        if (ImmersionBar.hasNavigationBar(getActivity())) {
            ImmersionBar.with(this)
                    .transparentNavigationBar()  //透明导航栏，不写默认黑色(设置此方法，fullScreen()方法自动为true)
//                    .navigationBarColor(R.color.colorAccent)//导航栏颜色，不写默认黑色
//                    .navigationBarAlpha(0.4f)  //导航栏透明度，不写默认0.0F
//                    .navigationBarDarkIcon(true) //导航栏图标是深色，不写默认为亮色
                    .fullScreen(false)      //有导航栏的情况下，activity全屏显示，也就是activity最下面被导航栏覆盖，不写默认非全屏
                    .init();
        }*/
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        mUnBinder.unbind();
    }

    protected abstract int getLayoutId();

    protected abstract void initEventAndData();

    /**
     * 显示隐藏进度条
     */
    @Override
    public synchronized void showProgressDialog(String messege) {
        if (TextUtils.isEmpty(messege)) messege = "加载中...";
        builder = new LoadingDialog.Builder(getActivity())
                .setMessage(messege)
                .setCancelable(false);
        if (dialog == null) {
            dialog = builder.create();
            dialog.show();
        } else {
            dialog.show();
        }
    }

    /**
     * 隐藏进度条
     */
    @Override
    public synchronized void dimissProgressDialog() {
        if (dialog != null)
            dialog.dismiss();
    }

    @Override
    public void useNightMode(boolean isNight) {

    }

}
